package cn.flaty.views.web.users;

import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import cn.flaty.services.user.entity.User;
import cn.flaty.services.user.service.UserBaseService;
import cn.flaty.services.user.vo.UserVo;
import cn.flaty.views.web.BaseController;
import cn.flaty.views.web.WebConstants;


@Controller
@RequestMapping("/users")
@SessionAttributes({WebConstants.SECURIYT_CODE,WebConstants.SESSION_USER})
public class UserBaseController extends BaseController {

	private UserBaseService userBaseService;

	/**
	 * 注册页面
	 * @author flatychen
	 * @date 2014-4-15
	 * @return
	 */
	@RequestMapping(value = "/new", method = RequestMethod.GET)
	public String register(String aab,String abc) {
		return "/views/users/new";
	}

	/**
	 * 注册处理,从session取数据
	 * @author flatychen
	 * @date 2014-4-15
	 * @param user
	 * @param result
	 * @return
	 */
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String create(@ModelAttribute(WebConstants.SECURIYT_CODE) String code,@Valid UserVo user, BindingResult result) {
		
		// 服务端验证 
		
		if (result.hasErrors()) {
			System.out.println(result.getFieldError().getField() + ","
					+ result.getFieldError().getDefaultMessage());
			return "redirect:new";
		}
		
		// 进行注册
		user.setServerValidateCode(code); // 把验证码交给serive业务去验证
		userBaseService.register(user);
		
		
		return "/views/users/register";
	}

	/**
	 * 登陆页面
	 * @author flatychen
	 * @date 2014-4-15
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String loginPage() {
		return "/views/users/login";
	}
	
	/**
	 * 登陆,把用户放入session
	 * @author flatychen
	 * @date 2014-4-15
	 * @param userVo
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public ModelAndView login(@Valid UserVo userVo) {
		Map<String,Object> maps = super.getMapWapper();
		// 登陆
		User user = userBaseService.login(userVo);
		// 这里会把user 放入session
		maps.put(WebConstants.SESSION_USER, user);
		
		return new ModelAndView("/views/users/loginSuccess", maps);
	}
	
	/**
	 * 列出所有
	 * @author flatychen
	 * @date 2014-4-15
	 * @return
	 */
	@RequestMapping(value="/index",method = RequestMethod.GET)
	public ModelAndView index() {
		return new ModelAndView("/views/users/index", "users",
				userBaseService.userList());
	}

	/**
	 * 查看用户详情
	 * @author flatychen
	 * @date 2014-4-15
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/show/{id}", method = RequestMethod.GET)
	public String show(@PathVariable Long id) {
		User u = new User();
		u.setEmail("flatychen@163.com");
		u.setPhone("18122834850");
		u.setPwd("password");
		return "/views/users/show";
	}
	
	
	/**
	 * 复杂查询
	 * @author flatychen
	 * @date 2014-4-15
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/query", method = RequestMethod.GET)
	public String query(User user,Model model) {
		model.addAttribute("user", user);
		return "/views/users/query";
	}

}
